/*
visqrt
Copyright (C) 2010  Bhupendra Aole

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
*/

package aole.math;

public class Color3 {
	public float r, g, b;

	public static final Color3 BLACK = new Color3(0, 0, 0);
	public static final Color3 RED = new Color3(1, 0, 0);
	public static final Color3 GREEN = new Color3(0, 1, 0);
	public static final Color3 BLUE = new Color3(0, 0, 1);
	public static final Color3 WHITE = new Color3(1, 1, 1);

	public Color3() {
		r = g = b = 0f;
	}

	public Color3(Color3 v) {
		r = v.r;
		g = v.g;
		b = v.b;
	}

	public Color3(float r, float g, float b) {
		this.r = r;
		this.g = g;
		this.b = b;
	}

	public Color3(double r, double g, double b) {
		this.r = (float) r;
		this.g = (float) g;
		this.b = (float) b;
	}

	public void setZero () {
		r = g = b = 0f;
	}

	public void set (float r, float g, float b) {
		this.r = r;
		this.g = g;
		this.b = b;
	}

	public void set (Color3 v) {
		r = v.r;
		g = v.g;
		b = v.b;
	}

	public void limit (float min, float max) {
		if (r < min)
			r = min;
		if (g < min)
			g = min;
		if (b < min)
			b = min;

		if (r > max)
			r = max;
		if (g > max)
			g = max;
		if (b > max)
			b = max;
	}

	public void add (Color3 v) {
		r += v.r;
		g += v.g;
		b += v.b;
	}

	public void subtract (Color3 v) {
		r -= v.r;
		g -= v.g;
		b -= v.b;
	}

	public Color3 getAdded (Color3 v) {
		return new Color3(r + v.r, g + v.g, b + v.b);
	}

	public Color3 getSubtracted (Color3 v) {
		return new Color3(r - v.r, g - v.g, b - v.b);
	}

	public Color3 getScaled (float s) {
		return new Color3(r * s, g * s, b * s);
	}

	public Color3 getDivided (float s) {
		return new Color3(r / s, g / s, b / s);
	}

	public static Color3 add (Color3 v1, Color3 v2) {
		return new Color3(v1.r + v2.r, v1.g + v2.g, v1.b + v2.b);
	}

	public static Color3 multiply (Color3 v1, Color3 v2) {
		return new Color3(v1.r * v2.r, v1.g * v2.g, v1.b * v2.b);
	}

	@Override
	public String toString () {
		return "<" + r + "," + g + "," + b + ">";
	}

	public void scale (float f) {
		r *= f;
		g *= f;
		b *= f;
	}

	public void add (float f) {
		r += f;
		g += f;
		b += f;
	}

	public void read (String s) {
		String c[] = s.split(",");
		r = Float.parseFloat(c[0]);
		g = Float.parseFloat(c[1]);
		b = Float.parseFloat(c[2]);
	}

	public static Color3 getRandom () {
		return new Color3(Math.random(), Math.random(), Math.random());
	}
}
